package ruting;

import java.util.Arrays;
import java.util.HashSet;

public class _100460_使数组元素等于零 {

    public static void main(String[] args) {
        int[] nums = {2,3,4,0,4,1,0};
    }

     class Solution {
        public  int countValidSelections(int[] nums) {
            int res = 0;
            for (int i = 0; i < nums.length; i++) {
                if(nums[i] == 0){
                    HashSet<Integer> set = new HashSet<>();
                    set.add(i);
                    int[] temp1 = Arrays.copyOf(nums, nums.length);
                    int[] temp2 = Arrays.copyOf(nums, nums.length);
                    boolean left = left(temp1,   set, i - 1,i + 1);
                    boolean right = right(temp2, set,i - 1,i + 1);
                    if (left)
                        res++;
                    if (right)
                        res++;
                }
            }
            return res;
        }
        public  boolean left(int[] nums, HashSet<Integer> seen,int l,int r) {
            int left = l;
            while(left >= 0){
                if(nums[left] == 0){
                    if(!seen.contains(left)){
                        seen.add(left);
                    }
                    left--;
                    if(seen.size() == nums.length)
                        return true;
                }else if (nums[left] > 0){
                    nums[left]--;
                    if(nums[left] == 0){
                        seen.add(left);
                        left -= 1;
                    }
                    if (right(nums,seen,left,r)){
                        return true;
                    }else {
                        return false;
                    }
                }
            }
            return seen.size() == nums.length;
        }

        public  boolean right(int[] nums, HashSet<Integer> seen,int l,int r) {
            int right = r;
            while(right < nums.length){
                if(nums[right] == 0){
                    if(!seen.contains(right)){
                        seen.add(right);
                    }
                    right++;
                    if(seen.size() == nums.length)
                        return true;
                }else if (nums[right] > 0){
                    nums[right]--;
                    if(nums[right] == 0){
                        seen.add(right);
                        right += 1;
                    }
                    if (left(nums,seen,l,right)){
                        return true;
                    }else {
                        return false;
                    }
                }
            }
            return seen.size() == nums.length;
        }
    }
}
